﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace UCenter.ContactService.Email {
    public class EmailSinaVipService : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "vip.sina.com"; }
        }

        public string StartUrl {
            get { return "http://vip.sina.com.cn/cgi-bin/login.php"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("utf-8"); }
        }
        #endregion

        #region Events

        #endregion


        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            string returnUrl = StartUrl;
            CookieContainer cc = new CookieContainer();
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://vip.sina.com.cn/index.html";
            string contentType = "application/x-www-form-urlencoded";
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("u", userName);
            nvc.Add("psw", pwd);
            nvc.Add("domain", EmailSystem);
            string postContent = "u=binyzhang&domain=vip.sina.com&psw=321456789";
            string html = WebOpHelper.Post(StartUrl, accept, contentType, refer, language, postContent, cc, Encoding.GetEncoding("gb2312"), out returnUrl);
            Uri u = new Uri(returnUrl);
            string url = "http://" + u.Host + "/classic/addr_list_meta.php?check_time=" + Guid.NewGuid().ToString();
            html = WebOpHelper.GetHtml(url, accept, contentType, refer, language, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            string emails = WebOpHelper.Resove(html, "<xmp id='metadata'>", "</xmp>");
            string[] es = emails.Split(new char[] { '\r', '\n' });
            if (es.Length == 4) {
                string emailString = es[1];
                string[] esi = emailString.Split('`');

                foreach (string s in esi) {
                    if (!string.IsNullOrEmpty(s)) {
                        string[] em = s.Split('|');
                        if (em.Length >= 3) {
                            MailContract m = new MailContract();
                            m.Email = em[2];
                            m.Name = em[1];
                            mc.Add(m);
                        }
                    }
                }

            }
            return mc;
        }
    }
}
